<div class="page-header">
    <h1>
        Form Generate SN
        <small>
            <i class="icon-double-angle-right"></i>
            Generate SN Baru
        </small>
    </h1>
</div>

<div class="row">
    <div class="col-sm-7">
        
            <!-- PAGE CONTENT BEGINS -->
            <div class="widget-main">
            <form class="form-horizontal" role="form" action="" method="post">
                    <label class="inline">
                            <span class="lbl"> Jumlah SN: &nbsp; <input type="text" id="num" name="num" required /></span>
                    </label>

                    <button type="button" class="btn btn-success btn-sm" onclick="generateSN()">
                            Generate
                    </button>
            </form>
            </div>

        <br/>

        <table id="grid-table"></table>

        <div id="grid-pager"></div>

        <script type="text/javascript">
                var $path_base = "/";//this will be used in gritter alerts containing images
        </script>

        <!-- PAGE CONTENT ENDS -->
    </div>
</div>
<script>


    function validateNumber(evt) {

        var charCode = (evt.which) ? evt.which : event.keyCode
        if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;
        return true;
      }
      
    function downloadExcel(){

       var num = $('#num').val();
       
       $("#sn").datagrid("loading");

        $.ajax({
        type: 'GET',
        dataType: 'text',
        url: '<?php echo $this->url() ?>/excel/gensn',
        data: {jenis: jenis, date1: date1, date2: date2},
        success: function(result){
                    $("#sn").datagrid ("loaded");

                    if (result == "TIMEOUT"){
                        location.href = "<?php echo $this->url() ?>/admin";
                    }else{
                        location.href = "<?php echo $this->url() ?>/excel/"+result;
                    }
             }
        });

    }

    function cek(){
        var msg = [];

        if ($('#num').val() == "" || $('#num').val() <=0 ) msg.push('* Jumlah SN harus diisi dan lebih besar dari 0');
        if (msg.length == 0) return true;
        else{
            //alert("Data tidak lengkap:\n\n" + msg);
            //$.messager.alert("Data tidak lengkap:\n\n" + msg);
            alert(msg.join("\n"));
            return false;
        }
    }

    function generateSN(){

            if(cek()){

                var num = $('#num').val();

                jQuery("#grid-table").jqGrid('setGridParam',{
                    url:"<?php echo $this->url() ?>/../../jsondata/gensn?num="+num
                }).trigger("reloadGrid");
            }                    
    }

   //////////////////////////////


    jQuery(function($) {
            
            jQuery('#grid-table').jqGrid({
                url:'blank.html',
                datatype: "json",
                height: 150,
                colNames:['SN','PIN'],
                colModel:[
                    {name:'SN',index:'SN', width:200},
                    {name:'PIN',index:'PIN', width:200}
                ],
                rowNum:0,
                //rowList:[10,20,30],
                //pager: '#grid-pager',
                sortname: 'sn',
                viewrecords: true,
                sortorder: "desc",
                autowidth: true,
                rownumbers: true,
                caption:"Generate New SN"
            });

            //jQuery('#grid-table').jqGrid('navGrid','#grid-pager',{edit:false,add:false,del:false});

          

    });

</script>




